package engine.display.newAttack
{
	import engine.control.MiniLoadingBar;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.loader.SwfLoader;

	public class AttackSkillManage
	{
		private static var _self:AttackSkillManage;
		
		public static function getInstance():AttackSkillManage
		{
			if(_self==null)
			{
				_self=new AttackSkillManage();
			}
			return _self;
		}

		private var _bar			: MiniLoadingBar;
	
		private var _currentSkill	: String;
		
		private var _skillName		: String;
		
		private var _cacheList		: Array = [];
		
		private var _waitList		: Array = [];
		private var _waitNameList	: Array = [];
		
		private var _max			: int;
		
		private var _onComplete	: Function;
		
		public function AttackSkillManage()
		{
			_bar	=	MiniLoadingBar.getInstance();
		}
		
		public function loadSkills( skills : Array, onComplete : Function ):void
		{
			//普通攻击
			skills.push( "81000001" );
			
			//气满特效
			skills.push( "82009999" );
			skills.push(StaticIni.getIniVar("attackPetSkill","skill"))
			skills.push("7301177");
			skills.push("7301156");
			skills.push("7300167");
			_waitNameList = [];
			
			_waitList  = getEffects( skills );
		
			_max = _waitList.length;
			
			_onComplete = onComplete;
			
			startLoad();
		}
			
		private function getEffects( skills : Array ):Array
		{
			var effects : Array = [];
			
			var effectObj : Object;
			for each( var skill : String in skills )
			{
				//不是null，说明是buffer
				effectObj =  StaticIni.getIniObj( skill );
					
				if( effectObj != null && effectObj.hasOwnProperty( "EffectId" ) )
				{//是技能
					effectObj = StaticIni.getIniObj( StaticIni.getIniVar( skill, "EffectId" ) );
				}
				
				if( null != effectObj )
				{
					if( effectObj.hasOwnProperty("beAttactEffect") )
					{
						effects.push( effectObj[ "beAttactEffect" ] );
						
						_waitNameList.push( skill );
					}
					if( effectObj.hasOwnProperty("skin") )
					{
						effects.push( effectObj[ "skin" ] );
						
						_waitNameList.push( skill );
					}
				}
			}
			return effects;
		}
		
		private function startLoad():void
		{
			if( _waitList.length == 0 )
			{
				_onComplete();
				_bar.close();
				return;
			}
			
			_currentSkill = _waitList.shift();
			_skillName = StaticIni.getSwfLangStr( _waitNameList.shift() );
			
			if( _cacheList.indexOf( _currentSkill ) != -1 )
			{
				_max --;
				startLoad();
				return;
			}
			
			_bar.show("加载中");
			
			_cacheList.push( _currentSkill );
			
			SwfLoader.StartLoad( getSkillUrl( _currentSkill ), null, onComplete, [loadingHandle] );
		}
		
		private function getSkillUrl( skillId : String ):String
		{
			return "res/effect/" + skillId + ".swf";
		}
		
		private function onComplete(  val:DisplayObject, plist:Array=null ):void
		{
			if( val != null )
			{
				ResManager.SaveRes( _currentSkill, val );
			}
			
			if(  0 < _waitList.length )
			{
				startLoad();
			}else
			{
				_onComplete.call();
				_bar.close();
			}
			
		}
		private function loadingHandle( evt : Event ):void
		{
			var pe : ProgressEvent = evt as ProgressEvent;
			if( null != pe )
			{
				_bar.progress = Number( ( pe.bytesLoaded / pe.bytesTotal* ( _max - _waitList.length ) / _max ).toFixed(2) );
				_bar.title	=	"加载技能<" + _skillName + ">......   进度:" + ( _max - _waitList.length ) + " /" +  _max;
			}
		}
	}
}